﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class ExplotionObject : MonoBehaviour
{
    public void Play()
    {
        var particleSys = GetComponent<ParticleSystem>();
        if (particleSys == null)
            return;
        particleSys.Play();
        Destroy(gameObject, particleSys.main.duration + 1);
    }
    public void SetParticleScale(GameObject obj, float scale)
    {
        var particle = obj.GetComponent<ParticleSystem>();
        if (particle)
        {
            particle.transform.localScale *= scale;
            var main = particle.main;
            if (!particle.isPlaying)
            {
                main.duration *= scale;
                var colorS = main.startColor;
                var color = colorS.color;
                color.a *= scale;
                colorS.color = color;
                main.startColor = colorS;
            }
            var f = main.startLifetime;
            f.constant *= scale;
            f.constantMin *= scale;
            f.constantMax *= scale;
        }

        var audio = obj.GetComponent<AudioSource>();
        if (audio)
            audio.volume *= scale;
        for (int i = 0; i < obj.transform.childCount; ++i)
        {
            SetParticleScale(obj.transform.GetChild(i).gameObject, scale);
        }
    }
}